<?php

namespace helper;

/**
 * Class XmlGenerator
 * @package helper
 *
 *
 * add by arke.wu 2022-08-18 18:47 php数组转xml
 */
class XmlGenerator
{
    public $xml;
    public $indent;
    public $stack = [];

    /**
     * @param string $indent
     * @return $this
     *
     * 生成 XML 的头部信息
     */
    public function xmlWriter($indent = '')
    {
        $this->indent = $indent;
        $this->xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
        return $this;
    }


    /**
     * @param $element
     * @param array $attributes
     * @return $this
     *
     * 添加元素
     */
    public function push($element, $attributes = [])
    {
        $this->indent();
        $this->xml .= '<' . $element;
        foreach ($attributes as $key => $value) {
            $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
        }
        $this->xml .= ">";
        $this->stack[] = $element;
        return $this;
    }


    /**
     * @param $element
     * @param $content
     * @param array $attributes
     * @return $this
     *
     * @desc 属性
     */
    public function element($element, $content, $attributes = [])
    {
        $this->indent();
        $this->xml .= '<' . $element;
        foreach ($attributes as $key => $value) {
            $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
        }
        $this->xml .= '>' . htmlentities($content) . '</' . $element . '>' . "\n";
        return $this;
    }

    /**
     * @return $this
     *
     */
    public function pop()
    {
        $element = array_pop($this->stack);
        $this->indent();
        if ($element) {
            $this->xml .= "</$element>\n";
        }
        return $this;
    }

    /**
     * @desc 获取最终xml
     */
    public function getXml()
    {
        return $this->xml;
    }

    /**
     * @desc 缩进
     */
    protected function indent()
    {
        for ($i = 0, $j = count($this->stack); $i < $j; $i++) {
            $this->xml .= $this->indent;
        }
        return $this;
    }


    /**
     * @param $array
     * @param string $tag
     * @param array $attributes
     * @return $this
     */
    public function buildXMLFilterMulti($array,  $tag = '', $attributes = [])
    {
        $xmlFilter = "";
        foreach ($array as $key => $value) {
            if ($tag) {
                $key = $tag;
            }
            if (isset($attributes[$key])) {
                $attribute = $attributes[$key]['name'] . '="' . $attributes[$key]['value'] . '" ';
            } else {
                $attribute = "";
            }
            if (is_array($value)) {
                if (isset($value[0])) {//以数字索引的
                    $xmlFilter .= $this->createXMLFilterMulti($value, $key, $attributes);
                } else {
                    if (empty($attribute)) {
                        $xmlFilter .= " <$key>\n" . $this->createXMLFilterMulti($value, '', $attributes) . "</$key>\n";
                    } else {
                        $xmlFilter .= " <$key $attribute>\n" . $this->createXMLFilterMulti($value, '', $attributes) . "</$key>\n";
                    }

                }
            } else {
                if (intval($key) != 0 || $key === 0) {
                    $xmlFilter .= $value;
                } else {
                    if (empty($attribute)) {
                        $xmlFilter .= " <$key>$value</$key>\n";
                    } else {
                        $xmlFilter .= " <$key $attribute>$value</$key>\n";
                    }

                }
            }
        }
        //因为xml不能使用&，要转成&amp;
        $this->xml .= str_replace("&", "&amp;", $xmlFilter);

        return $this;
    }


    /**
     * @param $array
     * @param string $tag
     * @param array $attributes
     * @return string
     *
     * 用于回调
     */
    public function createXMLFilterMulti($array, $tag = '', $attributes = [])
    {
        $xmlFilter = "";
        foreach ($array as $key => $value) {
            if ($tag) {
                $key = $tag;
            }
            if (isset($attributes[$key])) {
                $attribute = ' ' . $attributes[$key]['name'] . '="' . $attributes[$key]['value'] . '" ';
            } else {
                $attribute = "";
            }
            if (is_array($value)) {
                if (isset($value[0])) {//以数字索引的
                    $xmlFilter .= $this->createXMLFilterMulti($value, $key, $attributes);
                } else {
                    if (empty($attribute)) {
                        $xmlFilter .= " <$key>\n" . $this->createXMLFilterMulti($value, '', $attributes) . "</$key>\n";
                    } else {
                        $xmlFilter .= " <$key $attribute>\n" . $this->createXMLFilterMulti($value, '', $attributes) . "</$key>\n";
                    }

                }
            } else {
                if (intval($key) != 0 || $key === 0) {
                    $xmlFilter .= $value;
                } else {
                    if (empty($attribute)) {
                        $xmlFilter .= " <$key>$value</$key>\n";
                    } else {
                        $xmlFilter .= " <$key $attribute>$value</$key>\n";
                    }
                }
            }
        }
        return $xmlFilter;
    }

}
